package delivery

import "learning/gooop/saga/mqs/models"


type tMsgHistoryRing struct {
	items []*models.QueuedMsg
	capacity int
	index int
}

func newMsgHistoryRing(capacity int) iMsgHistoryRing {
	it := new(tMsgHistoryRing)
	it.init(capacity)
	return it
}

func (me *tMsgHistoryRing) init(capacity int) {
	me.items = make([]*models.QueuedMsg, capacity)
	me.capacity = capacity
	me.index = 0
}


func (me *tMsgHistoryRing) Has(id int) bool {
	for _,it := range me.items {
		if it != nil && it.ID == id {
			return true
		}
	}

	return false
}

func (me *tMsgHistoryRing) Push(msg *models.QueuedMsg) {
	me.items[me.index] = msg

	me.index++
	if me.index >= me.capacity {
		me.index = 0
	}
}